/**
 *  CSV Parser
 *  Luciano RUBIO <luciano@loociano.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "CSVParser.hpp"
#include "Year.hpp"

/**
 * Main function
 */
int main()
{
	ifstream file("detailed.csv");

	if (file.is_open())
	{
		CSVRow row;
		Year year;
		Date *date = NULL;

		while (file >> row)
		{
			if (row[0] == kDate)
			{
				if (row[1] != "")
				{
					// Create date
					date = new Date(row[1]);
					year.addDate(date);
				}
			}

			// Detect item by mealtime
			if (row[0] == kBreakfast || row[0] == kMorningSnack
					|| row[0] == kLunch || row[0] == kAfternoonSnack
					|| row[0] == kDinner || row[0] == kMidnightSnack
					|| row[0] == k4AM)
			{
				Item item;
				item.setMeal(row[0]);

				try
				{
					if (row[1] != "")
						item.setBrand(row[1]);
					if (row[2] != "")
						item.setName(row[2]);
					if (row[3] != "")
						item.setServings(item.parseString(row[3]));
					if (row[4] != "")
						item.setCaloriesIn(item.parseString(row[4]));
					if (row[5] != "")
						item.setSugarsIn(item.parseString(row[5]));
					if (row[6] != "")
						item.setCarbsIn(item.parseString(row[6]));
					if (row[7] != "")
						item.setFatsIn(item.parseString(row[7]));
					if (row[8] != "")
						item.setProteinIn(item.parseString(row[8]));
					if (row[9] != "")
						item.setCholesterolIn(item.parseString(row[9]));
					if (row[10] != "")
						item.setSodiumIn(item.parseString(row[10]));
					if (row[11] != "")
						item.setFiberIn(item.parseString(row[11]));
					if (row[12] != "")
						item.setCalories(item.parseString(row[12]));
					if (row[13] != "")
						item.setSugars(item.parseString(row[13]));
					if (row[14] != "")
						item.setCarbs(item.parseString(row[14]));
					if (row[15] != "")
						item.setFats(item.parseString(row[15]));
					if (row[16] != "")
						item.setProtein(item.parseString(row[16]));
					if (row[17] != "")
						item.setCholesterol(item.parseString(row[17]));
					if (row[18] != "")
						item.setSodium(item.parseString(row[18]));
					if (row[19] != "")
						item.setFiber(item.parseString(row[19]));

				} catch (out_of_range& oor)
				{
					cerr << "Out of Range error: " << oor.what() << endl;
					item.printData();

				} catch (...) 
                {
                    cerr << "Exception" << endl;
                }

				date->addItem(item);
			}
		}

		// Print stats
		//year.printTotalDates();
		//year.printDates();
		cout << "Oatmeal: " << year.countItem("oatmeal") << endl;
		cout << "Total items: " << year.countItems() << endl;
	}
	else
	{
		cout << "Error opening file" << endl;
	}
	return 0;
}

